<template>
  <div class="info" v-loading="loading">
    <div class="card_con">
      <div class="row mb15">
        <span class="l" @click="handleAdd">
          <i class="el-icon-circle-plus-outline"></i>新增套餐
        </span>
      </div>
      <el-table
        border
        :data="tableData"
        :header-cell-style="{
          'text-align': 'center',
        }"
        :cell-style="{ 'text-align': 'center' }"
        style="width: 100%"
        highlight-current-row
      >
        <!-- <el-table-column prop="id" label="代金券id" min-width="10%">
        </el-table-column> -->
        <el-table-column prop="name" label="套餐名" min-width="20%">
        </el-table-column>
        <el-table-column label="缩略图" min-width="15%">
          <template slot-scope="scope">
            <div class="slt">
              <el-popover placement="right" width="200" trigger="hover">
                <img class="tbImg1" :src="scope.row.thumb" alt="" />
                <img
                  class="tbImg"
                  slot="reference"
                  :src="scope.row.thumb"
                  alt=""
                />
              </el-popover>
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="oprice" label="原价" min-width="10%">
        </el-table-column> -->
        <el-table-column prop="price" label="现价" min-width="10%">
        </el-table-column>
        <el-table-column prop="use_range" label="使用范围" min-width="10%">
        </el-table-column>
        <el-table-column prop="stock" label="库存" min-width="10%">
        </el-table-column>
        <el-table-column prop="status" label="状态" min-width="10%">
          <template slot-scope="scope">
            <div v-if="scope.row.status === 1" class="green">开启</div>
            <div v-if="scope.row.status === 0" class="red">禁用</div>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" min-width="20%">
        </el-table-column>
        <el-table-column label="操作" min-width="20%">
          <template slot-scope="scope">
            <div class="caozuo">
              <div class="handleEdit hand_cs" @click="handleEdit(scope.row)">
                编辑
              </div>
              <div
                class="handleDelete red hand_cs"
                @click="handleDelete(scope.row)"
              >
                删除
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <Pagination :page="page" @onCurrentChange="handleCurrentChange" />
      </div>
      <!-- 编辑 添加 -->
      <el-dialog
        class="editDialog"
        :title="title"
        :visible.sync="dialogVisible"
        width="800px"
        :close-on-click-modal="false"
      >
        <Dialog
          v-if="dialogVisible"
          :type="2"
          :rowData="rowData"
          @handleClose="handleClose"
        ></Dialog>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import Dialog from "./add_edit_dialog.vue";
import Pagination from "@/components/Pagination.vue";
import { comboList, delCombo } from "@/api/shopSystem";
export default {
  components: { Pagination, Dialog },
  data() {
    return {
      loading: false,
      tableData: [],
      page: {
        total: 0,
        pageNum: 1, // 当前页数
        pageSize: 10, // 每页的条数
      },
      dialogVisible: false,

      title: "",
      rowId: "", //当前行id
      rowData: "",
    };
  },
  methods: {
    //列表
    async comboList() {
      const { data, err } = await comboList({
        page: this.page.pageNum,
      });
      if (err === 0) {
        this.loading = false;
        this.tableData = data.data;
        this.page.total = data.total;
        this.page.pageSize = data.per_page;
      }
    },
    //删除
    async delCombo(id) {
      const { err } = await delCombo({
        cm_id: id,
      });
      if (err === 0) {
        this.$message({
          showClose: true,
          message: "操作成功！",
          type: "success",
        });
        this.comboList();
      } else {
        this.$message({
          showClose: true,
          message: "操作失败！",
          type: "error",
        });
      }
    },
    //新增
    handleAdd() {
      this.rowData = {};
      this.dialogVisible = true;
      this.title = "新增套餐";
    },
    //编辑
    handleEdit(row) {
      this.rowData = row;
      this.dialogVisible = true;
      this.title = "编辑套餐";
    },

    //删除
    handleDelete(row) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delCombo(row.id);
        })
        .catch(() => {
          ("1111111111111");
        });
    },
    // 当前页: ${val}
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.loading = true;
      this.comboList();
    },
    //关闭添加编辑
    handleClose() {
      this.dialogVisible = false;
      this.comboList();
    },
  },
  created() {
    this.comboList();
  },
};
</script>

<style lang="scss" scoped>
@import "@/style/index.scss";

.pagination {
  display: flex;
  justify-content: flex-end;
  padding-top: 10px;
}

.row {
  display: flex;
  border-radius: 2px;
  //   border: 1px solid #ccc;
  .l {
    padding: 8px 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #ccc;
    font-size: 13px;
    i {
      margin-right: 6px;
      font-size: 13px;
    }
    &:hover {
      cursor: pointer;
    }
  }
}
.editDialog {
  //   z-index: 2005 !important;
  /deep/.el-dialog {
    height: 75%;
    .el-dialog__body {
      height: 80%;
      overflow: auto;
    }
  }
}
.handleEdit {
  margin-left: 20px;
}
.handleDelete {
  margin-left: 20px;
}

/deep/.el-table .el-table__cell {
  padding: 10px 0;
}
.search_data {
  /deep/.el-input__inner {
    height: 36px;
    line-height: 36px;
    font-size: 13px;
  }
}

.beizhu {
  padding: 0 20px 50px 20px;
  box-sizing: border-box;
  .bz {
    margin-bottom: 10px;
    margin-top: 20px;
  }
  .btn {
    margin-top: 20px;
    .el-button {
      width: 100%;
    }
  }
}
</style>
<style>
.v-modal {
  z-index: 2000 !important;
}
</style>
